import Vue from 'vue';
import Router from 'vue-router';
// import Home from './components/Home.vue'
import Login from './views/Login.vue';

import store from './store/index';

Vue.use(Router);

const router = new Router({
    // mode: 'history',
    // base: PerformanceObserverEntryList.env.BASE_URL,
    routes: [
      {
        path: '/',
        name: 'Login',
        component: Login,
        meta: {
          requireAuth: true,
        }
      }
    ]
});

router.beforeEach((to, from, next) => {
  // ...
  console.log('to:::', to);
  console.log('from:::', from);
  if(to.path == '/') {
    next();
  } else {
    const token = sessionStorage.getItem('token');
    console.log('token::', token);
    if(token) {
      console.log('store:routerList:', store.state.permission.routeList);
      if(store.state.permission.routeList) {
        next();
      } else {
        
        const permissionId = sessionStorage.getItem('permissionId');
        store.dispatch('GET_USER_PERMISSION', {
          permissionId,
        }).then(() => {
          // console.log('res::', res);
          // console.log('router::::', router);
          // router.addRoutes(res);
          // next();
          next({ ...to, replace: true })
        })
        
      }
    } else {
      next({ path: '/' })
    }
  }
})

export default router;